/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "EntityView.h"

#include "../kernel.h"

extern Kernel* g_kernel;

EntityView::EntityView(IEntity* ent)
{
    m_owner = ent;

    if ( m_owner )
    {
        if ( m_owner->m_attributes.Has( "dialog" ) )
        {
            m_speakerIcon = g_kernel->GetResource()->LoadSingleImage( "misc/speakerIcon.tga" );
        }
    }
}


/**
======================
Update the model to represent the correct 
Face of the entity
======================
*/
void EntityView::Update(long deltaTime)
{
    m_model->SetCurrentDirection( m_owner->GetFace() );
    m_model->SetCurrentState( m_owner->GetState() );
    
    // update the model
    m_model->Update(deltaTime); 
}

/**
======================
Render this view
======================
*/
void EntityView::Render( IRender *r, ICamera* cam )
{
    Vector2f offsetPos( m_owner->m_world - cam->GetPosition() );
    offsetPos.Set( offsetPos.x + cam->GetViewPort().x, offsetPos.y + cam->GetViewPort().y );

    // draw the model
    r->DrawSubImage(    &m_model->GetImage(),           // image of character
                        offsetPos.x,                    // x position
                        offsetPos.y,                    // y position
                        m_model->GetRow(),              // Row of animation
                        m_model->GetCurrentFrame()      // current frame
                   );

    r->SetColor(1, 1, 1 );

    if ( m_owner->GetState() == ENT_STATES[TALKING] )
    {
        Rect bounds = m_owner->GetBounds();
        r->DrawImage( &m_speakerIcon, offsetPos.x + bounds.width / 2 + 20, offsetPos.y - bounds.height );
    }

    // show useful stats
    if ( g_kernel->GetCvars()->GetFloatValue("debug") ) 
    {
        r->DrawString(  offsetPos.x, offsetPos.y-30, m_owner->GetName().c_str() );
        r->DrawString(  offsetPos.x, offsetPos.y-10, "%6.2f,%6.2f", m_owner->m_world.x, m_owner->m_world.y );

 
        r->DrawString( offsetPos.x, offsetPos.y+80, "HP: %4d", m_owner->m_health );
        r->DrawString( offsetPos.x, offsetPos.y+100, ("State: "+m_owner->GetState()).c_str() );
        //r->DrawRect( m_owner->GetBounds().x, m_owner->GetBounds().y, m_owner->GetBounds().width, m_owner->GetBounds().height );
        r->DrawRect( offsetPos.x, offsetPos.y + m_owner->GetBounds().height, m_owner->GetBounds().width, m_owner->GetBounds().height );

        Rect rect = m_owner->GetReachRectangle();
        r->DrawRect( rect.x, rect.y, rect.width, rect.height );
    }
}

/**
========================
Apply an effect
========================
*/
void EntityView::Effect(int Flags)
{
    // TODO
}


EntityView::~EntityView(void)
{
}
